﻿using System;
using System.Net.Sockets;
using System.Reflection;
using System.Linq;

namespace MTP.Core
{

    /// <summary>
    /// 远端配置
    /// </summary>
    public class RemoteOptions
    {
        public static RemoteOptions Builder(string configStr)
        {
            var array = configStr.Split(',');
            return new RemoteOptions(array[0], array.Skip(1).ToArray());
        }

        public RemoteOptions(string mtpUrl, params string[] serviceNames)
        {
            this.MtpUrl = mtpUrl;
            this.ServiceNames = serviceNames;
        }


        /// <summary>
        /// 密钥
        /// </summary>
        public string GetCipher()
        {
            if (string.IsNullOrEmpty(MtpUrl))
            {
                throw new ArgumentNullException("mtpUrl");
            }
            var index = MtpUrl.IndexOf("//") + 2;
            var mtp = MtpUrl.Substring(index);
            var nextIndex = mtp.IndexOf("/") + 1;
            if (nextIndex <= 0)
            {
                return "MTP";
            }
            var next = mtp.Substring(nextIndex);
            if (string.IsNullOrEmpty(next))
            {
                return "MTP";
            }
            return next;
        }

        /// <summary>
        /// 远端 地址
        /// </summary>
        public string MtpUrl { get; set; }


        public AddressFamily AddressFamily { get; set; } = AddressFamily.InterNetwork;

        /// <summary>
        /// 远端服务名称
        /// </summary>
        public string[] ServiceNames { get; set; } = new string[0];
    }
}
